<script setup lang="ts">
import { onLaunch, onShow, onHide } from '@dcloudio/uni-app'
import 'abortcontroller-polyfill/dist/abortcontroller-polyfill-only'
import { useProfileStore } from './store'
import { mplogin } from './service/login/login'

const setToken = async () => {
  const { toggleTheme } = useTheme()
  const profileStore = useProfileStore()
  const { mpLoginParams, operate } = profileStore
  let isDriver = false
  toggleTheme()
  if (operate === 'store') {
    isDriver = false
  }
  if (operate === 'driver') {
    isDriver = true
  }
  if (mpLoginParams.username && mpLoginParams.password) {
    const data = { ...mpLoginParams, isDriver }
    const response = await mplogin(data)
    if (response.success) {
      if (response.result.token) {
        if (mpLoginParams.isDriver) {
          uni.switchTab({
            url: '/pages/index-driver/index-driver',
          })
        } else {
          uni.switchTab({
            url: '/pages/index/index',
          })
        }
        profileStore.setProfileInfo(response.result)
      }
    } else {
      uni.showToast({
        icon: 'none',
        title: response.message,
      })
    }
  }
}

onLaunch(async () => {
  console.log('App Launch')
  // const profileStore = useProfileStore()
  // const { openid, setOpenid } = profileStore

  // if (!openid) {
  //   uni.login({
  //     service: 'oauth',
  //     success: async (res) => {
  //       const response = await jscode2session(res.code)
  //       if (response.success) {
  //         const openid = response.result.openid
  //         if (openid) {
  //           setOpenid(openid)
  //           await setToken()
  //           // uni.$emit('openid', {})
  //         }
  //       }
  //     },
  //   })
  // } else {
  //   await setToken()
  //   // uni.$emit('openid', {})
  // }
  await setToken()
})
onShow(async () => {
  console.log('App Show')
})
onHide(() => {
  console.log('App Hide')
})
</script>

<style lang="scss">
:root,
page {
  // 修改按主题色
  // --wot-color-theme: #e93b3d;
  // 修改按钮背景色
  // --wot-button-primary-bg-color: green;
  background-color: #f7f7f7;
}
/* stylelint-disable selector-type-no-unknown */
button::after {
  border: none;
}

swiper,
scroll-view {
  flex: 1;
  height: 100%;
  overflow: hidden;
}
image {
  width: 100%;
  height: 100%;
  vertical-align: middle;
}

// 单行省略，优先使用 unocss: text-ellipsis
.ellipsis {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

// 两行省略
.ellipsis-2 {
  display: -webkit-box;
  overflow: hidden;
  text-overflow: ellipsis;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

// 三行省略
.ellipsis-3 {
  display: -webkit-box;
  overflow: hidden;
  text-overflow: ellipsis;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}
</style>
